package com.sx.pipeline.ui.register

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.config.IdentityType
import com.sx.pipeline.app.config.RegisterType
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.RegisterDTO
import com.sx.pipeline.repos.http.res.*

/**
 * @Author: YY
 * @Date: 2022/4/23
 * @Description: TODO
 */
class RegisterVM(app: Application) : CYBaseViewModel(app) {

    /**
     * 获取单位类型
     */
    val unitTypeListEvent: SingleLiveEvent<List<UnitTypeVO>?> = SingleLiveEvent()
    fun getUnitType(identityID :Int?=null) {
        addSubscribe(mModel.queryUnitType(identityID)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<List<UnitTypeVO>, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: List<UnitTypeVO>) {
                    unitTypeListEvent.postValue(result)
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort("$msg")
                    unitTypeListEvent.postValue(null)
                }
            })
        )
    }


    /**
     * 查询单位列表
     */
    val unitNameListEvent: SingleLiveEvent<List<UnitNameVO>> = SingleLiveEvent()
    fun getUnitNameList() {
        if (areaIDField.get() == 0) {
            ToastUtils.showShort("请选择单位所属地址")
            return
        }
        addSubscribe(
            mModel.getRegUnitNameList(areaId = locationId.get()!!, identity = identityField.get()!!, isPage = false)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<UnitNameVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<UnitNameVO>) {
                        unitNameListEvent.postValue(result.records)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }


    // 注册类型
    val registerTypeField: ObservableField<Int> = ObservableField()
    // 注册身份
    val identityField: ObservableField<Int> = ObservableField()

    // 省ID
    val provinceIDField: ObservableField<Int> = ObservableField(0)
    // 市ID
    val cityIDField: ObservableField<Int> = ObservableField(0)
    // 区ID
    val areaIDField: ObservableField<Int> = ObservableField(0)
    //分类区域ID
    val locationId: ObservableField<Int> = ObservableField(0)
    // 详细地址
    val addressField: ObservableField<String> = ObservableField("")

    // 单位类型ID
    val unitTypeIdsField: ObservableField<String> = ObservableField("")
    // 单位类型名称
    val unitTypeNameField: ObservableField<String> = ObservableField("")
    // 类型属性
    val typeAttributeField: ObservableField<String> = ObservableField("")

    // 单位名称
    val unitNameField: ObservableField<String> = ObservableField("")
    // 选择得单位id
    val selectUnitIdField: ObservableField<Long> = ObservableField()
    val registerSuccessEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()

    /**
     * 单位注册
     * @param name 姓名
     * @param phone 手机号
     * @param smsCode 验证码
     * @param identifier 纳税人识别号
     * @param unitName 单位名称
     */
    fun unitRegister(name: String, phone: String, smsCode: String, identifier: String, unitName: String, isCheck: Boolean) {
        Logs.d("unitTypeIdsField: ${unitTypeIdsField.get()}")
        Logs.d("unitTypeNameField: ${unitTypeNameField.get()}")
        Logs.d("typeAttributeField: ${typeAttributeField.get()}")
        if (TextUtils.isEmpty(typeAttributeField.get())) {
            unitNameField.set(unitName)
        } else {
            unitNameField.set("${unitTypeNameField.get()} ${typeAttributeField.get()}")
        }
        Logs.d("unitName: ${unitNameField.get()}")

        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号")
            return
        }
        if (TextUtils.isEmpty(smsCode)) {
            ToastUtils.showShort("请输入验证码")
            return
        }
        if (TextUtils.isEmpty(addressField.get())) {
            ToastUtils.showShort("请选择单位地址")
            return
        }
        if (TextUtils.isEmpty(unitNameField.get())) {
            ToastUtils.showShort("请填写单位名称")
            return
        }
//        if (identityField.get()!!.toInt() != IdentityType.OPERATING_UNIT.code) {
//            if (TextUtils.isEmpty(identifier)) {
//                ToastUtils.showShort("请输入纳税人识别号")
//                return
//            }
//        }
        if (!isCheck) {
            ToastUtils.showShort("请阅读并同意相关政策协议")
            return
        }

        val dto = RegisterDTO (
            registerTypeField.get()!!.toInt(),
            identityField.get(),
            provinceIDField.get()!!.toInt(),
            cityIDField.get()!!.toInt(),
            areaIDField.get()!!.toInt(),
            addressField.get()!!,
            name, phone, smsCode, identifier,
            unitTypeIdsField.get()!!,
            typeAttributeField.get(),
            unitNameField.get()!!
        )

        addSubscribe(mModel.register(dto)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Boolean, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: Boolean) {
                    registerSuccessEvent.postValue(true)
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort("$msg")
                }
            })
        )
    }

    /**
     * 个人注册
     */

    fun personalRegister(name: String, phone: String, smsCode: String, superiorName: String,
                         superiorPhone: String, isCheck: Boolean) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号")
            return
        }
        if (TextUtils.isEmpty(smsCode)) {
            ToastUtils.showShort("请输入验证码")
            return
        }
//        if (TextUtils.isEmpty(addressField.get())) {
//            ToastUtils.showShort("请选择单位地址")
//            return
//        }
        if (!isCheck) {
            ToastUtils.showShort("请阅读并同意相关政策协议")
            return
        }

        val dto = RegisterDTO (
            registerTypeField.get()!!.toInt(),
            identityField.get()!!.toInt(),
            provinceIDField.get()!!.toInt(),
            cityIDField.get()!!.toInt(),
            areaIDField.get()!!.toInt(),
            addressField.get()!!,
            name, phone, smsCode,
            unitTypeIds = unitTypeIdsField.get(),
            typeAttribute = typeAttributeField.get(),
            unitName = unitNameField.get(),
            unitId = selectUnitIdField.get(),
            parentName = superiorName,
            parentPhone = superiorPhone
        )

        addSubscribe(mModel.register(dto)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Boolean, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: Boolean) {
                    registerSuccessEvent.postValue(true)
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort("$msg")
                }
            })
        )
    }
}